package com.hitqz.robot.biz.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hitqz.robot.api.common.entity.SysWarning;
import com.hitqz.robot.api.business.model.param.sysWarning.QuerySysWarningParams;
import com.hitqz.robot.api.business.model.vo.sysWarning.SysWarningVo;
import com.hitqz.robot.biz.service.SysWarningService;
import com.hitqz.robot.common.core.util.R;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * @author xupkun
 * @date 2024/6/25
 */

@RestController
@AllArgsConstructor
@RequestMapping("/sysWarning")
@Tag(name = "告警管理")
public class SysWarningController  {

    private final SysWarningService sysWarningService;

    @PostMapping("/page")
    @Operation(summary = "分页查询告警数据")
    public R<Page<SysWarningVo>> page(@RequestBody QuerySysWarningParams querySysWarningParams) {
        Page page = new Page<>(querySysWarningParams.getCurrent(),querySysWarningParams.getSize());
        Page<SysWarning> data = sysWarningService.page(page,
                Wrappers.<SysWarning>lambdaQuery()
                        .like(StrUtil.isNotBlank(querySysWarningParams.getContent()), SysWarning::getContent,
                                querySysWarningParams.getContent())
                        .orderByDesc(SysWarning::getId));
        Page<SysWarningVo> voPage = new Page<>();
        BeanUtil.copyProperties(data, voPage, false);

        return R.ok(voPage);
    }

    @GetMapping(value = "read/{id}")
    @Operation(summary = "某条告警已经读取")
    public R read(@PathVariable Integer id) {
        SysWarning sysWarning = sysWarningService.getById(id);
        if (sysWarning == null) {
            return R.failed("data not found");
        }
        sysWarning.setStatus(1);
        sysWarningService.updateById(sysWarning);
        return R.ok();
    }


    @GetMapping(value = "readAll")
    @Operation(summary = "全部告警读取")
    public R readAll() {
        sysWarningService.update(Wrappers.<SysWarning>lambdaUpdate()
                .set(SysWarning::getStatus, 1)
                .eq(SysWarning::getStatus, 0));
        return R.ok();
    }


    @GetMapping(value = "sync")
    @Operation(summary = "同步上传告警数据")
    public R sync() {
        sysWarningService.syncRobotWarnings(null);
        return R.ok();
    }


}
